﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entities;
using DAL;

namespace SurveyGUI
{
    public partial class UpdateInspectForm : Form
    {
        IInspectRepository rep = new InspectRepository();
        int MaTT = 0;
        string TenTT = "";
        string GioiTinh = "";
        string ChucVu = "";
        string TenDN = "";
        string MK = "";
        //int Ktra = 0;


        public UpdateInspectForm()
        {
            InitializeComponent();
        }

        //gan cac gia tri tu 1 hang cua DataGridView len cac control cua form UpdateInspect
        public UpdateInspectForm(int ma, string ten, string gt, string cv, string tdn, string mk, int kt)
            : this()
        {
            this.MaTT = ma;
            this.TenTT = ten;
            this.GioiTinh = gt;
            this.ChucVu = cv;
            this.TenDN = tdn;
            this.MK = mk;
            //this.Ktra = kt;

            txtMa.Text = ma.ToString();
            txtTen.Text = ten;
            if (gt == "Nam")
                RblNam.Checked = true;
            else
                RblNu.Checked = true;
            if (cv == "Nhân Viên Kiểm Định")
                cbbChucVu.Text = "Nhân Viên Kiểm Định";
            else if (cv == "Khảo Sát Viên")
                cbbChucVu.Text = "Khảo Sát Viên";
            else if (cv == "Trưởng Phòng Kiểm Định")
                cbbChucVu.Text = "Trưởng Phòng Kiểm Định";
            txtTenDN.Text = tdn;
            txtMK.Text = mk;
            //lblKTra.Text = kt.ToString();
        }

        private void btnXoaTrang_Click(object sender, EventArgs e)
        {
            txtTen.Text = "";
            cbbChucVu.Text = comboKhaoSatVien.Text;
            RblNam.Checked = true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //Bien lay gia tri ten than tra tu form UpdateInspect len form UpdateInspectAccount
        public string tenTT
        {
            get { return txtTen.Text; }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (txtTen.Text == "")
                MessageBox.Show("Chưa nhập tên thanh tra");
            else
            {
                //----------------------------Khong chay-----------------
                //Inspect inspect = new Inspect();
                //inspect.MaTT = int.Parse(txtMa.Text);
                //inspect.TenTT = txtTen.Text;
                //inspect.ChucVu = cbbChucVu.SelectedItem.ToString();
                //string gt = "";
                //if (RblNam.Checked)
                //    gt = "Nam";
                //else
                //    gt = "Nữ";
                //inspect.GioiTinh = gt;
                //rep.Update(inspect);

                int id = int.Parse(txtMa.Text);
                Inspect i = rep.GetById(id);
                string ten = txtTen.Text;
                string cv = cbbChucVu.SelectedItem.ToString();
                string gt;
                if (RblNam.Checked)
                    gt = "Nam";
                else
                    gt = "Nữ";
                string tdn = txtTenDN.Text;
                string mk = txtMK.Text;
                //int kt = int.Parse(lblKTra.Text);
                rep.Update(i, id, ten, cv, gt, tdn, mk, 2);
                MessageBox.Show("Cập nhật thành công");
                this.Close();
            }
        }

        private void UpdateInspectForm_Load(object sender, EventArgs e)
        {
            this.AcceptButton = btnOK;
        }

        private void UpdateInspectForm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnOK.PerformClick();
            }
        }
    }
}
